package com.hmdp.config;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class RabbitMQConfig {

    // 交换机
    public final static String EXCHANGE = "hmdpTopicExchange";
    // 绑定键
    public final static String MSG_QUEUE = "hmdp.queue";

    @Bean
    public Queue msgQueue() {
        return new Queue(MSG_QUEUE);
    }


    @Bean
    TopicExchange exchange() {
        return new TopicExchange(EXCHANGE);
    }


    @Bean
    Binding bindingExchangeMessage1() {
        return BindingBuilder.bind(msgQueue()).to(exchange()).with(MSG_QUEUE);
    }


    /**
     * 将自定义的RabbitTemplate对象注入bean容器
     *
     * @param connectionFactory
     * @return
     */
    @Bean
    public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        //设置开启消息推送结果回调
        rabbitTemplate.setMandatory(true);
        //设置ConfirmCallback回调
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                log.info("==============ConfirmCallback start ===============");
                log.info("回调数据：{}", correlationData);
                log.info("确认结果：{}", ack);
                log.info("返回原因：{}", cause);
                log.info("==============ConfirmCallback end =================");
            }
        });
        //设置ReturnCallback回调
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                log.info("==============ReturnCallback start ===============");
                log.info("发送消息：{}", JSONUtil.toJsonStr(message));
                log.info("结果状态码：{}", replyCode);
                log.info("结果状态信息：{}", replyText);
                log.info("交换机：{}", exchange);
                log.info("路由key：{}", routingKey);
                log.info("==============ReturnCallback end =================");
            }
        });
        return rabbitTemplate;
    }
}
